import textReflect.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Text {
    public static void main(String[] args) {
       //newInstanceReflect();
        //fieldReflect();
        //constructorReflect();
        methodReflect();
    }

    public static void methodReflect(){
        try {
            Class<?> c1 = Class.forName("textReflect.Student");
            Method function = c1.getDeclaredMethod("function", String.class);
            Student student =(Student) c1.newInstance();
            function.setAccessible(true);
            function.invoke(student,"调用了该方法...");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }



    public static void constructorReflect(){
        try {
            Class<?> c1 = Class.forName("textReflect.Student");
            Constructor<?> declaredConstructor =
                    c1.getDeclaredConstructor(String.class, int.class);
            declaredConstructor.setAccessible(true);
            Student student = (Student)
                    declaredConstructor.newInstance("123", 12);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }



    public static void fieldReflect(){
        try {
            Class<?> c1 = Class.forName("textReflect.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);  //想要访问私有的属性得授权
            Student student = (Student) c1.newInstance();
            field.set(student,"110");   //设置实例中私有成员的值
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }


    public static void newInstanceReflect(){
        try{
            Class<?> aClass =Class.forName("textReflect.Student");
            Student student = (Student) aClass.newInstance(); //创建类的实例
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main1(String[] args) {
        try{
            Class<?> s1 = Class.forName("textReflect.Student");  //方法1
            Class<?> s2 = Student.class;                            //方法2
            Student student =new Student();                         //方法3
            Class<?> s3 = student.getClass();
            System.out.println(s1 == s2);
            System.out.println(s2 == s3);
            System.out.println(s1 == s3);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
